Certified Java Programmer Mock Exam


Question 1


class L {
  public static void main (String s[]) {
    int i = 1 + 2 | 1 << 2 ^ 1 | 1;
    System.out.println(i%5);
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: 1
b. Prints: 2
c. Prints: 3
d. Prints: 4
e. Prints: 5
f. Runtime error
g. Compiler error
h. None of the above

Question 2


class L {
  public static void main (String s[]) {
    int i = 1 | 2 ^ 3 >> 2 & 14 + 2;
    System.out.println(i);
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: 1
b. Prints: 2
c. Prints: 3
d. Prints: 4
e. Prints: 5
f. Runtime error
g. Compiler error
h. None of the above

Question 3


class A {
  static int m(int i, String s) {
    System.out.print("["+s+","+i+"]"+",");
    return i;
  }
  public static void main (String[] args) {
    int i = 0;
    int a[] = new int[3];
    a[m(i++,"a")] = m(i++,"b");
    System.out.print(a[0]+","+a[1]+","+a[2] +","+ i);
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: [a,0],[b,1],1,0,0,2
b. Prints: [a,1],[b,2],0,2,0,2
c. Prints: [a,1],[b,0],0,0,0,2
d. Prints: [a,2],[b,1],0,0,1,2
e. Runtime error
f. Compiler error
g. None of the above

Question 4


class T {
  public static void main(String args[]) {
    String a = "1";
    byte b = 2;
    short c = 3;
    char d = 4;
    int e = 5;
    float f = 2;
    a += b *= c += d *= e += f;
    System.out.print(a);
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: 57
b. Prints: 63
c. Prints: 162
d. Runtime error
e. Compiler error
f. None of the above

Question 5


class M {
  static int m(int i) {
    System.out.print(i + ", ");
    return i;
  }

  public static void main(String s[]) {
    int i=0;
    System.out.print(m(++i) | m(++i) & m(++i) + m(++i) % m(++i));
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: 1,2,3,4,5,1
b. Prints: 1,2,3,4,5,2
c. Prints: 1,2,3,4,5,3
d. Prints: 1,2,3,4,5,4
e. Prints: 1,2,3,4,5,5
f. Prints: 1,2,3,4,5,6
g. Runtime error
h. Compiler error
i. None of the above

Question 6


class D {
  static int m1(int i, String s) {
    System.out.print("["+s+","+i+"]"+",");
    return i;
  }

  static int m2(int i, String s) {
    System.out.print("["+s+","+i+"]"+",");
    return 0;
  }
  public static void main (String[] args) {
    int i = 0;
    i = m1(++i,"A") + m2(i,"B") + m1(i++,"C") + m2(i,"D");
    System.out.print(i);
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: [A,0],[B,1],[C,1],[D,2],1
b. Prints: [A,1],[B,1],[C,1],[D,2],2
c. Prints: [A,1],[B,1],[C,1],[D,2],3
d. Prints: [A,1],[B,1],[C,1],[D,2],5
e. Runtime error
f. Compiler error
g. None of the above

Question 7


class B {
  static int x;
  static int m(int i) {
    System.out.print(i + ",");
    x += i;
    return 0;
  }
  public static void main (String[] args) {
    int i = 0;
    int j = 0;
    int k = 0;
    i = j = k = i++ + m(i) + j++ + m(j) + k++ + m(k);
    System.out.print(i + j + k + "," + x);
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: 0,0,0,3,0
b. Prints: 1,1,1,0,3
c. Prints: 1,1,1,3,3
d. Prints: 1,1,1,6,3
e. Prints: 1,1,1,9,3
f. Prints: 1,1
g. Runtime error
h. Compiler error
i. None of the above

Question 8


class A {
  static int m(int i, String s) {
    System.out.print("["+s+","+i+"]"+",");
    return i;
  }
  public static void main (String[] args) {
    int i = 0;
    int a[] = {0,1,2};
    a[m(i++,"a")] = a[m(i++,"b")] = a[m(i++,"c")];
    System.out.print(a[0]+","+a[1]+","+a[2]);
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: [a,0],[b,1],[c,2],2,2,2
b. Prints: [a,0],[b,1],[c,2],0,0,0
c. Prints: [a,2],[b,1],[c,0],0,0,0
d. Prints: [a,2],[b,1],[c,0],2,2,2
e. Runtime error
f. Compiler error
g. None of the above

Question 9


class M {
  static int m(int i) {
    System.out.print(i + ", ");
    return i;
  }
  public static void main(String s[]) {
    int i=0;
    int j=0;
    System.out.print(m(++i) | m(++i) & m(++i) >> m(++j) / m(++j) + m(++j));
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: 1,2,3,4,5,6,1
b. Prints: 1,2,3,1,2,3,1
c. Prints: 1,2,3,4,5,6,2
d. Prints: 1,2,3,1,2,3,2
e. Prints: 1,2,3,4,5,6,3
f. Prints: 1,2,3,1,2,3,3
g. Runtime error
h. Compiler error
i. None of the above

Question 10


class C {
  static int m(int i) {
    System.out.print(i + ", ");
    return i;
  }

  public static void main(String s[]) {
    int i = 1;
    m(m(++i) - m(i++) + ~m(-i) * -m(~i));
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: 2, 2, -3, -4, 8,
b. Prints: 2, 3, -3, -4, 7,
c. Prints: 1, 1, 1, 1, 0,
d. Prints: 2, 2, -2, -2, 4,
e. Prints: 2, 3, -2, -2, 3,
f. Prints: -1, -2, 2, 2, 0,
g. Runtime error
h. Compiler error
i. None of the above

Question 11


class F {
  public static void main (String []s) {
    int i = 1;
    i += ~i - -i * ++i + i-- % ++i * i++;
    System.out.print(i);
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: -1
b. Prints: 0
c. Prints: 1
d. Prints: 2
e. Prints: 3
f. Prints: 4
g. Runtime error
h. Compiler error
i. None of the above


Copyright © 2002, Dan Chisholm
All rights reserved.
1